package com.jweb.framework.core.util;

import java.io.IOException;
import java.io.Writer;


public class FilterWriterE extends FilterWriterB {
	
	private final boolean a;
    private boolean b;
    
    public FilterWriterE(Writer writer, String s, boolean flag)
    {
        super(writer);
        b = false;
        a = flag;
    }

    public void write(int i)
        throws IOException
    {
        if(i <= 62)
        {
            switch(i)
            {
            case 60: // '<'
                out.write("&lt;");
                break;

            case 38: // '&'
                out.write("&amp;");
                break;

            case 62: // '>'
                if(b)
                    out.write("&gt;");
                else
                    out.write(i);
                break;

            case 13: // '\r'
                if(a)
                    out.write("&#13;");
                else
                    out.write(i);
                break;

            default:
                out.write(i);
                break;
            }
            b = false;
        } else
        {
            out.write(i);
            b = i == 93;
        }
    }

    public void write(char ac[], int i, int j)
        throws IOException
    {
        if(j < 2)
        {
            if(j == 1)
                write(ac[i]);
            return;
        }
        char c = '\0';
        j += i;
        if(b)
        {
            c = ac[i];
            if(c == '>')
            {
                out.write("&gt;");
                i++;
            }
        }
        do
        {
            int k = i;
            String s = null;
            for(; i < j; i++)
            {
                c = ac[i];
                if(c > '>')
                    continue;
                if(c == '<')
                {
                    s = "&lt;";
                    break;
                }
                if(c == '&')
                {
                    s = "&amp;";
                    break;
                }
                if(c == '\r')
                {
                    if(!a)
                        continue;
                    s = "&#13;";
                    break;
                }
                if(c != '>' || i <= k || ac[i - 1] != ']')
                    continue;
                s = "&gt;";
                break;
            }

            int l = i - k;
            if(l > 0)
                out.write(ac, k, l);
            if(s != null)
            {
                out.write(s);
                s = null;
            }
        } while(++i < j);
        b = c == ']';
    }

    public void write(String s, int i, int j)
        throws IOException
    {
        if(j < 2)
        {
            if(j == 1)
                write(s.charAt(i));
            return;
        }
        char c = '\0';
        j += i;
        if(b)
        {
            c = s.charAt(i);
            if(c == '>')
            {
                out.write("&gt;");
                i++;
            }
        }
        do
        {
            int k = i;
            String s1 = null;
            for(; i < j; i++)
            {
                c = s.charAt(i);
                if(c > '>')
                    continue;
                if(c == '<')
                {
                    s1 = "&lt;";
                    break;
                }
                if(c == '&')
                {
                    s1 = "&amp;";
                    break;
                }
                if(c == '\r')
                {
                    if(!a)
                        continue;
                    s1 = "&#13;";
                    break;
                }
                if(c != '>' || i <= k || s.charAt(i - 1) != ']')
                    continue;
                s1 = "&gt;";
                break;
            }

            int l = i - k;
            if(l > 0)
                out.write(s, k, l);
            if(s1 != null)
            {
                out.write(s1);
                s1 = null;
            }
        } while(++i < j);
        b = c == ']';
    }
	
}
